import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/home/message/message_controller.dart';
import 'package:flutter_changdian_football/utils/str_utils.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';

class MessageDialog {

  static void show(
    BuildContext mContext, {
      Function? confirmTap,
      int index = 0
    }
  ) {
    showDialog(
      barrierDismissible: false,
      context: mContext,
      builder: (BuildContext context) {
        return WillPopScope(
          onWillPop: () async => false,
          child: ShowDialogPage(
            confirmTap: confirmTap,
            index: index
          ),
        );
      }
    );
  }

}



class ShowDialogPage extends Dialog {
  ShowDialogPage({
    this.confirmTap,
    this.cancelTap,
    this.index = 0
  });

  // 确认
  final Function? confirmTap;
  final Function? cancelTap;
  final int index;

  @override
  Widget build(BuildContext dContext) {
    return Material(
      //创建透明层
      type: MaterialType.transparency, //透明类型
      //自定义dialog布局
      child: GetBuilder<MessageController>(
        builder: (messageController) {
          return Center(
            child: RichText(
              text: TextSpan(
                children: [
                  WidgetSpan(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Stack(
                          clipBehavior: Clip.none,
                          alignment: Alignment.center,
                          children: [
                            Container(
                              margin: EdgeInsets.symmetric(horizontal: 40.w),
                              width: double.infinity,
                              decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(12.r)
                              ),
                              child: Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  SizedBox(height: 46.w,),

                                  Container(
                                    alignment: Alignment.center,
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      children: [
                                        Text(messageController.messageList[index].sendUserNickname ?? '此用户暂无昵称', style: TextStyle(fontSize: 18.sp, color: const Color(0xff323232)),),
                                        SizedBox(width: 6.w,),
                                        SizedBox(
                                          width: 14.w,
                                          height: 14.w,
                                          child: Image.asset(StrUtils.getSexImage(messageController.messageList[index].sendUserSex ?? 0)),
                                        )
                                      ],
                                    ),
                                  ),

                                  SizedBox(height: 20.w,),


                                  Container(
                                    constraints: BoxConstraints(
                                      maxHeight: 240.w
                                    ),
                                    child: ListView.separated(
                                      itemCount: messageController.messageList[index].content?.split(' ').length ?? 0,
                                      padding: EdgeInsets.only(left: 12.w, right: 29.w),
                                      shrinkWrap: true,
                                      physics: const BouncingScrollPhysics(),
                                      separatorBuilder: (context, subIndex) {
                                        return SizedBox(height: 16.w,);
                                      },
                                      itemBuilder: (BuildContext context, int subIndex) {
                                        List list = messageController.messageList[index].content?.split(' ') ?? [];
                                        return Container(
                                          padding: EdgeInsets.symmetric(vertical: 7.w, horizontal: 12.w),
                                          decoration: BoxDecoration(
                                            color: const Color(0xffF8FAF9),
                                            borderRadius: BorderRadius.only(
                                              topRight: Radius.circular(15.r),
                                              bottomLeft: Radius.circular(15.r),
                                              bottomRight: Radius.circular(15.r)
                                            )
                                          ),
                                          child: Text(list[subIndex], style: TextStyle(color: const Color(0xff242941), fontSize: 12.sp, height: 1.5),),
                                        );
                                      },
                                    ),
                                  ),


                                  Container(
                                    padding: EdgeInsets.only(left: 12.w, right: 12.w, bottom: 10.w, top: 30.w),
                                    child: GestureDetector(
                                      behavior: HitTestBehavior.translucent,
                                      onTap: () {
                                        confirmTap?.call();
                                      },
                                      child: Container(
                                        padding: EdgeInsets.symmetric(vertical: 12.w),
                                        decoration: BoxDecoration(
                                          color: const Color(0xff499360),
                                          borderRadius: BorderRadius.circular(8.r),
                                          border: Border.all(
                                            width: 1.w,
                                            color: const Color(0xff499360)
                                          )
                                        ),
                                        alignment: Alignment.center,
                                        child: Text('确认', style: TextStyle(fontSize: 14.sp, color: Colors.white, fontWeight: FontWeight.w500),),
                                      ),
                                    ),
                                  )
                                ],
                              ),
                            ),

                            Positioned(
                              top: -34.w,
                              child: Container(
                                width: 68.w,
                                height: 68.w,
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(68.r)
                                ),
                                padding: EdgeInsets.all(2.w),
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(68.r),
                                  child: Image.network(messageController.messageList[index].sendUserImg ?? '', gaplessPlayback: true, excludeFromSemantics: true, fit: BoxFit.fill,)
                                ),
                              )
                            )
                          ],
                        ),

                        SizedBox(height: 60.w,),

                        GestureDetector(
                          behavior: HitTestBehavior.translucent,
                          onTap: () {
                            Get.back();
                          },
                          child: Container(
                            width: 35.w,
                            height: 35.w,
                            decoration: BoxDecoration(
                              border: Border.all(
                                width: 1.5.w,
                                color: const Color(0xffFFFFFF)
                              ),
                              borderRadius: BorderRadius.circular(35.r)
                            ),
                            child: const Icon(Icons.close, color: Colors.white,),
                          ),
                        )
                      ],
                    )
                  ),
                ]
              ),
            ),
          );
        }
      )
    );
  }
}





